package org.yoki.edu.portal.core.radius.domain;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yunzhitx.cloud.common.model.PageResult;
import com.yunzhitx.cloud.common.model.ddd.AbstractEntity;
import com.yunzhitx.cloud.common.model.ddd.InstanceFactory;
import com.yunzhitx.cloud.common.model.exception.BizException;
import lombok.Data;
import org.yoki.edu.portal.core.exception.BasicRestStatus;
import org.yoki.edu.portal.core.radius.infra.RadreplyRepository;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

/**
 * 代码生成器自动生成
 * Date:2017-10-29 17:24:25
 *
 * @author
 */
@Data
@Table(name = "radreply")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Radreply implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id; //
    @Column(name = "username")
    private String username; //
    @Column(name = "attribute")
    private String attribute; //
    @Column(name = "op")
    private String op; //
    @Column(name = "value")
    private String value; //

    public Radreply() {
    }

    public Radreply(Integer id) {
        setId(id);
    }

    public Serializable getKey() {
        return getId();
    }

    public boolean existed() {
        return false;
    }

    public void save() throws Exception {
        if (existed()) {
            throw new BizException(BasicRestStatus.repeatDataErrorStatus);
        }
        getRadreplyRepository().insert(this);
    }

    public static void delete(Integer[] ids) throws Exception {
        for (Integer id : ids) {
            getRadreplyRepository().deleteByPrimaryKey(id);
        }
    }

    public void update() throws Exception {
        getRadreplyRepository().updateByPrimaryKeySelective(this);
    }

    public Radreply detail() {
        return getRadreplyRepository().selectByPrimaryKey(getKey());
    }

    public static PageResult<Radreply> page(PageResult<Radreply> page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        PageInfo<Radreply> pageInfo = new PageInfo<>(getRadreplyRepository().selectAll());
        page.setTotal(pageInfo.getTotal());
        page.setPageData(pageInfo.getList());

        return page;
    }

    private static RadreplyRepository radreplyRepository;

    public static RadreplyRepository getRadreplyRepository() {
        if (null == radreplyRepository) {
            radreplyRepository = InstanceFactory.getInstance(RadreplyRepository.class);
        }
        return radreplyRepository;
    }

}